package modes

import (
	"fmt"
	"time"
)

/*听课记录*/
type Record struct {
	Id        int64  `json:"cord_id" xorm:"id"`                 //听课记录id
	UId       int64  `json:"cord_uid" xorm:"uid"`               //学员ID
	SId       int64  `json:"cord_sid" xorm:"sid"`               //课程id
	CId       int64  `json:"cord_cid" xorm:"cid"`               //班型id
	Name      string `json:"cord_name" xorm:"name"`             //课程名称
	StudyAt   int64  `json:"cord_study_at" xorm:"study_at"`     //学习时间
	LectureAt int64  `json:"cord_lecture_at" xorm:"lecture_at"` //听课时长
	TotalAt   int64  `json:"cord_total_at" xorm:"total_at"`     //总时长
	RUrl      string `json:"cord_url" xorm:"url"`               //资源url
	LecCount  int64  `json:"cord_lec_count" xorm:"lec_count"`   //听课次数
	At        int64  `json:"cord_at" xorm:"at"`                 //创建时间
}

func (this *Record) TableName() string {
	return "record"
}
func (this *Record) Save() (int64, error) {
	this.At = time.Now().Unix()
	this.LecCount++
	return Db(0).Insert(this)
}

func (this *Record) GetLog() (bool, error) {
	where := fmt.Sprintf("uid = %d and sid = %d", this.UId, this.SId)
	return Db(0).Where(where).Get(this)
}

func (this *Record) Get() (bool, error) {
	return Db(0).Get(this)
}
func (this *Record) delete(where string) (int64, error) {
	return Db(0).Where(where).Delete(this)
}
func (this *Record) Del() (int64, error) {
	where := fmt.Sprintf("id =%d", this.Id)
	return this.delete(where)
}
func (this *Record) Count(where string) (int64, error) {
	var val Record
	return Db(0).Where(where).Count(&val)
}
func (this *Record) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *Record) Set(field string) (int64, error) {
	where := fmt.Sprintf("uid = %d and sid = %d", this.UId, this.SId)
	return this.update(where, field)
}
func (this *Record) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *Record) where(fage, count, page int, where, field string) ([]Record, error) {
	list := make([]Record, 0)
	var err error
	if field == "" {
		field = "id"
	}
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}
func (this *Record) List(val *Pager) error {
	val.TName = this.TableName()
	w := fmt.Sprintf("uid = %d ", this.UId)
	val.Total, _ = this.Count(w)
	list, err := this.where(val.Rules, val.Count, val.Page-1, w, "")
	if err == nil {
		val.List = list
	}
	return err
}

type RecordResour struct {
	Record   `xorm:"extends"`
	Resource `xorm:"extends"`
}

func (this *Record) Lookups(val *Pager) error {
	list := make([]Record, 0)
	val.TName = "record"
	val.Page--
	var field, sort string
	switch val.Coll {
	case 0:
		field = fmt.Sprintf("%s.study_at", val.TName) // 时间排序
	default:
		field = fmt.Sprintf("%s.study_at", val.TName) // 状态
	}
	if 1 == val.Rules {
		sort = fmt.Sprintf("%s asc", field)
	} else {
		sort = fmt.Sprintf("%s desc", field)
	}
	val.Total, _ = Db(0).Table(val.TName).Where(val.ToWhere()).Count(this)
	err := Db(0).Table(val.TName).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err

}

type RecordSum struct {
	LectureAt string `json:"count"` //听课总时长
}

func (this *RecordSum) Counts(uid int64) ([]RecordSum, error) {
	list := make([]RecordSum, 0)
	selec := "select sum(lecture_at) as lecture FROM record"
	where := fmt.Sprintf("where uid = %d ", uid)
	sil, err := Db(0).Query(fmt.Sprintf(" %s %s", selec, where))
	if nil == err {
		for i, _ := range sil {
			this.LectureAt = string(sil[i]["lecture"])
			list = append(list, *this)
		}
	}
	fmt.Println(sil)
	return list, err
}

func (this *RecordSum) Countsss(uid int64) (int64, error) {
	count, err := Db(0).SumInt(&Record{UId: uid}, "lecture_at")
	if err != nil {
		return 0, err
	}
	fmt.Println(count, "-------")
	return count, err
}
